<?php

namespace app\tool\onlineChat;

use app\factory\Factory;
use app\modelOpreate\onlineChat\HistoryRecordOpe;
use app\modelOpreate\onlineChat\UserRoomOpe;
use app\tool\ToolBase;
use think\facade\Cache;

/**
 * ClassName:消息列表查询器
 * @author leijunxiang
 * @time 2024/12/5
 * @auditor
 */
class MsgListSelect extends ToolBase
{
    const TOOL_MSG_LIST_SELECT = "MsgListSelect";

    ##redis存储key
    //最新点赞数
    const LIKE = "_like";
    //最新评论数
    const COMMENT = "_Comment";
    //最新关注数
    const ATTENTION = "_attention";
    //最新@我的人数
    const AT = "_at";
    //未读数
    const NO_READ_MSG = "_unread";
    //redis工具对象
    protected $redisToolObj;
    //消息列表
    protected $msgArr;
    //用户id
    protected $userId;

    /**
     *
     */
    public function __construct()
    {
        $this->redisToolObj = $this->createRedisToolObj();
    }


    /**
     * (消息列表)开始查询(用户id)
     */
    public function startSelect($userId)
    {
        $arr = [];
        if ($userId) {
            //初始化数据
            $this->initData($userId);
            //生成消息列表
            $this->createMsgList();
            //生成其他数据
            $this->createElseData();
            $arr = $this->msgArr;
            //清除数据
            $this->cleanData();
        } else {
            $this->errCode = FAIL;
            $this->errMsg = "没查询到列表";
        }
        return $arr;
    }

    /**
     * 初始化数据
     */
    protected function initData($userId)
    {
        $this->init();
        //消息列表
        $this->msgArr = [];
        //用户id
        $this->userId = $userId;

    }

    /**
     * 生成消息列表
     */
    protected function createMsgList()
    {
        $arr = [];
        //生成房间号队列
        $toolObj = Factory::getInstance()->getToolObj(UserRoomOpe::TOOL_USER_ROOM_OPE);
        //历史记录工具
        $hrToolObj = Factory::getInstance()->getToolObj(HistoryRecordOpe::TOOL_HISTORY_RECORD_OPE);
        //获取该用户所有房间号(用户id)
        $allRoomNumbers = $toolObj->getAllRoomNumberByUserId($this->userId);
        if ($allRoomNumbers) {
            //循环
            foreach ($allRoomNumbers as $allRoomNumber) {
                $roomNumber = $allRoomNumber['roomNumber'];
                $msg = [];
                //房间号
                $msg['roomNumber'] = $roomNumber;
                //发送人头像
                $userIcon = $allRoomNumber['userIcon'];
                $msg['userIcon'] = $userIcon;
                //发送人名称
                $userName = $allRoomNumber['userName'];
                $msg['userName'] = $userName;
                //发送人用户id
                $userId = $allRoomNumber['userId'];
                $msg['userId'] = $userId;
                //获取最新发送时间和消息内容(房间号,用户id)
                $msgs = $hrToolObj->getNewSendTime($roomNumber, $this->userId);
                if ($msgs) {
                    //最新消息内容
                    $msgContent = $msgs['sendContent'];
                    $msg['msgContent'] = $msgContent;

                    //最新发送时间
                    $sendTime = $msgs['sendTime'];
                    $msg['sendTime'] = $sendTime;

                }else{
                    $msg['msgContent'] = '';
                    $msg['sendTime'] = 0;
                    $msg['userId'] = 0;
                }
                //生成未读消息数
                $noReadMsgNumber = $this->redisToolObj->hGet($this->userId.self::NO_READ_MSG, $roomNumber);
                if ($noReadMsgNumber) {
                    $msg['noReadMsgNumber'] = $noReadMsgNumber;
                } else {
                    $msg['noReadMsgNumber'] = 0;
                }
                $arr[] = $msg;
            }
        }
        //生成其他消息
        $this->msgArr['msgList'] = $arr;
    }

    /**
     * 生成其他数据
     */
    protected function createElseData()
    {
        //最新点赞
        $likeNumber = $this->redisToolObj->get($this->userId . self::LIKE);
        $this->msgArr['likeNumber'] = $likeNumber ? $likeNumber : 0;
        //最新评论
        $commentNumber = $this->redisToolObj->get($this->userId . self::COMMENT);
        $this->msgArr['commentNumber'] = $commentNumber ? $commentNumber : 0;
        //最新关注
        $attentionNumber = $this->redisToolObj->get($this->userId . self::ATTENTION);
        $this->msgArr['attentionNumber'] = $attentionNumber ? $attentionNumber : 0;
        //最新@我的
        $atMeNumber = $this->redisToolObj->get($this->userId . self::AT);
        $this->msgArr['atMeNumber'] = $atMeNumber ? $atMeNumber : 0;
    }

    /**
     * 清除数据
     */
    protected function cleanData()
    {
        //用户id
        $this->userId = "";
        $this->msgArr = [];
    }

    /**
     * 生成redis工具对象
     */
    protected function createRedisToolObj()
    {
        $redis = Cache::store('redis');
        return $redis;
    }


}